angular.module('starter.services', [])
    .factory('server', function ($http, $q, $state,$rootScope) {
        var storage = window.localStorage;
        var mobile = "192.168.191.1"
        var server = "10.180.79.110";
        var port = "8080";
        var url = "http://" + mobile + ':' + port + '/book_trade';
        var pay_url = "http://" + mobile + ':' + port + '/book_pay';
        user_info = function () {
            var defer = $q.defer();
            $http({
                url: url + '/user/userinfo',
                method: 'GET'
            }).success(function (response) {
                defer.resolve(response);
            })
            return defer.promise;
        }
        isLogin = function () {
            return storage.isLogin;
        }
        jumpTo = function (des) {
            $state.go(des, {}, { reload: true })
        }
        return {
            isLogin: function () {
                return isLogin();
            },
            needLogin: function () {
                if (storage.isLogin == "false") jumpTo("tab.sign_in");
            },
            jumpTo: function (des) {
                jumpTo(des);
            },
            user_info: function () {
                return user_info();
            },
            sign_out: function (user) {
                user.password = sha1(user.password);
                $http({
                    url: url + '/user/register',
                    method: 'POST',
                    data: user
                }).success(function () {
                    console.log("success");
                    jumpTo("tab.person_center");
                }).error(function () {
                    console.log("error");
                })
            },
            productlist: function () {
                var defer = $q.defer();
                $http.get(url + '/product/productlist')
                    .success(function (response) {
                        defer.resolve(response);
                    }
                    )
                return defer.promise;
            },
            seller_productlist: function () {
                var defer = $q.defer();
                $http.get(url + '/product/myproductlist')
                    .success(function (response) {
                        defer.resolve(response);
                    }
                    )
                return defer.promise;
            },
            sign_in: function (user) {
                user.password = sha1(user.password);
                $http({
                    url: url + '/user/login',
                    method: 'POST',
                    data: user
                }).success(function (response) {
                    storage.isLogin = true;
                   jumpTo("tab.person_center");
                }).error(function () {
                    console.log("Login error.");
                })
            },
            log_out: function () {
                storage.isLogin = false;
                $http.get(url + "/user/logout")
                    .success(function () {
                        jumpTo("tab.person_center");
                    })
            },
            modify_information: function (user) {
                user.oldpwd = sha1(user.oldpwd) || "";
                if (user.newpwd != "") user.newpwd = sha1(user.newpwd);
                console.log(user);
                $http({
                    url: url + '/user/modifyuserinfo',
                    method: 'POST',
                    data: user
                }).success(function () {
                    jumpTo("tab.sign_in");
                }).error(function () {
                    console.log("Modify error.");
                })
            },
            product_add: function (product) {
                console.log(product);
                $http({
                    url: url + '/product/addproduct',
                    method: 'POST',
                    data: product
                }).success(function () {
                    console.log("Add product success.");
                   jumpTo("tab.seller_management");
                }).error(function () {
                    console.log("Add product error.")
                })
            },
             product_modify: function (product) {
                console.log(product);
                $http({
                    url: url + '/product/modifyproduct',
                    method: 'POST',
                    data: product
                }).success(function () {
                    console.log("modify product success.");
                   jumpTo("tab.seller_management");
                }).error(function () {
                    console.log("Add product error.")
                })
            },
            orderlist: function () {
                var defer = $q.defer();
                $http.get(url + '/order/getuserorder')
                    .success(function (response) {
                        defer.resolve(response);
                    }
                    )
                return defer.promise;
            },
            seller_order: function () {
                var defer = $q.defer();
                $http.get(url + '/order/getsellorder')
                    .success(function (response) {
                        defer.resolve(response);
                    }
                    )
                return defer.promise;
            },
            shoppingcart: function () {
                if (!storage.shoppingcart) storage.shoppingcart = JSON.stringify({shoppingcart:[]});
                 var shoppingcart = JSON.parse(storage.shoppingcart);
                return shoppingcart;
            },
            add_to_shoppingcart: function (shopping) {
                 if (!storage.shoppingcart) storage.shoppingcart = JSON.stringify({shoppingcart:[]});
                 var shoppingcart = JSON.parse(storage.shoppingcart);
                 shoppingcart.shoppingcart.push(shopping);
                storage.shoppingcart = JSON.stringify(shoppingcart);
                jumpTo("tab.shoppingcart");
            },
            order_add: function (order) {
                console.log(order);
                $http({
                    url: url + '/product/buyproduct',
                    method: 'POST',
                    data: order
                }).success(function () {
                    console.log("Add order success.");
                    jumpTo("tab.order_management");
                }).error(function () {
                    console.log("Add order error.")
                })
            },
            order_modify: function (order_id, state) {
                $http.get(url + '/order/changeorderstate?order_id=' + order_id + '&state=' + state)
                    .success(function () {
                        console.log("Order modify success.");
                         jumpTo("tab.order_management");
                    })
                    .error(function () {
                        console.log("Order modify error.");
                    })
            },
            pay:function(p_user){
                 var defer = $q.defer();
                 $http({
                    url: pay_url + '/p_user/book_pay',
                    method: 'POST',
                    data: p_user
                }).success(function (response) {
                     defer.resolve(response);
                    jumpTo("tab.order_management");
                }).error(function () {
                    console.log("Pay error.")
                })
                return defer.promise;
            }
        }
    })


    ;
